﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace Presentation.Views.CustomControls
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:cust="clr-namespace:Presentation.Views.CustomControls"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:Presentation;assembly=Presentation"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:AutoCompleteTextBox/>
    ///
    /// </summary>
    public class AutoCompleteTextBox : TextBox
    {
        public IEnumerable<string> ItemsSource 
        {
            get { return (IEnumerable<string>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
        
        public static DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register
                (
                    "ItemsSource",
                    typeof(IEnumerable<string>),
                    typeof(AutoCompleteTextBox),
                    new PropertyMetadata(new List<string>())
                );
        //    ItemsControl.ItemsSourceProperty.AddOwner(
        //        typeof(AutoCompleteBox),
        //        new PropertyMetadata(new List<string>())
        //);


        public Boolean IsMatch
        {          
            get { return (Boolean)GetValue(IsMatchProperty); }
            private set { SetValue(IsMatchProperty, value); }
        }

        private List<string> _matches = new List<string>();

        public IEnumerable<string> Matches 
        {
            get { return _matches; }  
        }

        public static readonly DependencyProperty IsMatchProperty =
            DependencyProperty.Register
                (
                    "IsMatch",
                    typeof(Boolean),
                    typeof(AutoCompleteTextBox),
                    new PropertyMetadata(false)
                );

        static AutoCompleteTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AutoCompleteTextBox), new FrameworkPropertyMetadata(typeof(AutoCompleteTextBox)));
        }

        protected override void  OnTextChanged(TextChangedEventArgs e)
        {
            _matches.Clear();
            foreach (string value in ItemsSource)
            {
                if (value.IndexOf(Text, 0) == 0)
                {
                    _matches.Add(value);
                }
            }

            IsMatch = (_matches.Count > 0);

            base.OnTextChanged(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            BindingExpression bindingExpression = this.GetBindingExpression(TextBox.TextProperty);
            
            base.OnKeyDown(e);
        }
    }
}
